package com.zhuss.excecise.excecise;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.excecise.excecise.DetailExceciseBo;
import com.zhuss.bean.bo.excecise.excecise.SearchExceciseBo;
import com.zhuss.bean.bo.excecise.excecise.wechat.ListExceciseBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.excecise.excecise.DetailExceciseVo;
import com.zhuss.bean.vo.excecise.excecise.ListExceciseVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.excecise.processor.ExceciseAppendRead;
import com.zhuss.excecise.processor.ExceciseRead;
import com.zhuss.excecise.processor.ExceciseTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * excecise查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("exceciseReadService")
public class ExceciseReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("exceciseRead")
	private ExceciseRead exceciseRead;

	@Autowired
	@Qualifier("exceciseTopicRead")
	private ExceciseTopicRead exceciseTopicRead;
	
	@Autowired
	@Qualifier("exceciseAppendRead")
	private ExceciseAppendRead exceciseAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailExceciseBo bo = (DetailExceciseBo) param;
		Long exceciseId = bo.getExceciseId();
		DetailExceciseVo vo = (DetailExceciseVo) exceciseRead.detail(exceciseId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchExceciseBo bo = (SearchExceciseBo) param;
		Object result = exceciseRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailExceciseBo param) throws WpsException {
		Long exceciseId = param.getExceciseId();
		Object result = exceciseTopicRead.listTopicTree(exceciseId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listExcecise(Object param) throws WpsException {
		ListExceciseBo bo = (ListExceciseBo) param;
		Object result = exceciseRead.listExcecise(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailExceciseVo> pageVo = (PageVo<DetailExceciseVo>) result;
		for (DetailExceciseVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = exceciseAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getExceciseId(), null).size();
			Integer collectNum = exceciseAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getExceciseId(), null).size();
			Integer forwardNum = exceciseAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getExceciseId(), null).size();
			Integer recommendNum = exceciseAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getExceciseId(), null).size();
			Integer topicNum = exceciseTopicRead.listTopic(vo.getExceciseId(), 0l).size();
			
			Integer thumbSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getExceciseId(), userId).size();
			Integer collectSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getExceciseId(), userId).size();
			Integer forwardSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getExceciseId(), userId).size();
			Integer recommendSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getExceciseId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setRecommendNum(recommendNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			vo.setRecommendSelfNum(recommendSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListExceciseVo> pageVo = (PageVo<ListExceciseVo>) result;
		for (ListExceciseVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = exceciseAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getExceciseId(), null).size();
			Integer collectNum = exceciseAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getExceciseId(), null).size();
			Integer forwardNum = exceciseAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getExceciseId(), null).size();
			Integer recommendNum = exceciseAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getExceciseId(), null).size();
			Integer topicNum = exceciseTopicRead.listTopic(vo.getExceciseId(), 0l).size();
			
			Integer thumbSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getExceciseId(), userId).size();
			Integer collectSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getExceciseId(), userId).size();
			Integer forwardSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getExceciseId(), userId).size();
			Integer recommendSelfNum = exceciseAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getExceciseId(), userId).size();
			
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setRecommendNum(recommendNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			vo.setRecommendSelfNum(recommendSelfNum);
			
		}
	}
	
	
}

